/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sky.common.security.service;

import cn.hutool.core.util.StrUtil;
import com.sky.admin.api.entity.ReadClubStudentEntity;
import com.sky.admin.api.feign.RemoteUserService;
import com.sky.common.core.constant.CacheConstants;
import com.sky.common.core.constant.CommonConstants;
import com.sky.common.core.constant.SecurityConstants;
import com.sky.common.core.util.R;
import com.sky.common.core.util.RetOps;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * 用户详细信息
 *
 * @author lengleng hccake
 */
@Slf4j
@RequiredArgsConstructor
public class PigAppUserMobileDetailsServiceImpl implements PigUserDetailsService {

	private final RemoteUserService remoteUserService;

	private final CacheManager cacheManager;

	/**
	 * 手机号登录
	 *
	 * @param phone 手机号
	 * @return
	 */
	@Override
	@SneakyThrows
	public UserDetails loadUserByUsername(String phone) {
		Cache cache = cacheManager.getCache(CacheConstants.APP_USER_DETAILS);
		if (cache != null && cache.get(phone) != null) {
			return (PigUser) cache.get(phone).get();
		}

		R<ReadClubStudentEntity> result = remoteUserService.appUserMobileQuery(phone,
				SecurityConstants.FROM_IN);

		UserDetails userDetails = getAppUserDetails(result, phone);
		if (cache != null) {
			cache.put(phone, userDetails);
		}
		return userDetails;
	}

	private UserDetails getAppUserDetails(R<ReadClubStudentEntity> result, String phone) {
		ReadClubStudentEntity info = RetOps.of(result).getData()
				.orElseThrow(() -> new UsernameNotFoundException("用户不存在"));
		// 构造security用户
		return new PigUser(info.getUserId(), 0L, phone, SecurityConstants.BCRYPT + "Pass@1234",
				phone, true, true, true,
				StrUtil.equals(info.getLockFlag(), CommonConstants.STATUS_NORMAL),
				AuthorityUtils.NO_AUTHORITIES, info.getUserNameEn());
	}

	/**
	 * check-token 使用
	 *
	 * @param pigUser user
	 * @return
	 */
	@Override
	public UserDetails loadUserByUser(PigUser pigUser) {
		return this.loadUserByUsername(pigUser.getPhone());
	}

	/**
	 * 是否支持此客户端校验
	 *
	 * @param clientId 目标客户端
	 * @return true/false
	 */
	@Override
	public boolean support(String clientId, String grantType) {
		return SecurityConstants.MOBILE.equals(grantType)
				&& SecurityConstants.CLIENT_TYPE_WECHAT_MOBILE.equals(clientId);
	}

}
